/**
 * 
 */
package xx.services.teach;

import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import xx.services.Passport;
import xx.services.util.Constants;

/**
 * @author Administrator 2014-3-25
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/teach/upload")
public class Upload extends Passport
{

    /**
     * @param request
     */
    public Upload(@Context
    HttpServletRequest request)
    {
        super(request);
    }

    /**
     * 上传图片
     * 
     * @return
     */
    @POST
    public String uploadPhoto(@Context
            HttpServletResponse response )
    {
        try
        {
            String temp = request.getSession().getServletContext().getRealPath("/") + "temp"; // 临时目录
//            temp = "F:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp8\\wtpwebapps\\teach_service\\temp";
            System.out.println("temp=" + temp);
            File tempFile = new File(temp);
            String name = null;
            if(!tempFile.exists()){
                tempFile.mkdirs();
            }
            
            String loadpath = request.getSession().getServletContext().getRealPath("/") + "image"; // 上传文件存放目录
//            loadpath = "F:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp8\\wtpwebapps\\teach_service\\Image";
            System.out.println("loadpath=" + loadpath);
            File loadpathFile = new File(loadpath);
            if(!loadpathFile.exists()){
                loadpathFile.mkdirs();
            }
            
            DiskFileUpload fu = new DiskFileUpload();
            fu.setSizeMax(1 * 1024 * 1024); // 设置允许用户上传文件大小,单位:字节
            fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:字节
            fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录

            // 开始读取上传信息
            List fileItems = null;

            try
            {
                fileItems = fu.parseRequest(request);
                System.out.println("fileItems=" + fileItems);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件
            while (iter.hasNext())
            {
                FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表单信息
                if (!item.isFormField())
                {
                    name = item.getName();// 获取上传文件名,包括路径
                    name = name.substring(name.lastIndexOf("\\") + 1);// 从全路径中提取文件名
                    long size = item.getSize();
                    if ((name == null || name.equals("")) && size == 0)
                        continue;
                    int point = name.indexOf(".");
                    name = (new Date()).getTime() + name.substring(point, name.length());
                    File fNew = new File(loadpath, name);
                    try
                    {
                        item.write(fNew);
                    }
                    catch (Exception e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
                else
                // 取出不是文件域的所有表单信息
                {
                    // String fieldvalue = item.getString();
                    // 如果包含中文应写为：(转为UTF-8编码)
                    String fieldvalue = new String(item.getString().getBytes(), "UTF-8");
                }
            }
            String text1 = "11";
//            response.getWriter().write(loadpath+name);
//            response.sendRedirect("result.jsp?text1=" + text1);
            return ok(loadpath+"/"+name);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }

    }
    
}
